package com.kreao.korean.util;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.stereotype.Component;

/**
 * 加密工具类
 * 提供字符串加密和解密功能
 */
@Component
public class EncryptionUtil {

    /**
     * 创建加密器
     */
    private static StringEncryptor createEncryptor(String password) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        
        config.setPassword(password);
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        
        encryptor.setConfig(config);
        return encryptor;
    }

    /**
     * 加密字符串
     * @param plainText 明文
     * @param password 加密密钥
     * @return 加密后的字符串
     */
    public static String encrypt(String plainText, String password) {
        StringEncryptor encryptor = createEncryptor(password);
        return encryptor.encrypt(plainText);
    }

    /**
     * 解密字符串
     * @param encryptedText 密文
     * @param password 解密密钥
     * @return 解密后的字符串
     */
    public static String decrypt(String encryptedText, String password) {
        StringEncryptor encryptor = createEncryptor(password);
        return encryptor.decrypt(encryptedText);
    }

    /**
     * 生成Jasypt格式的加密字符串
     * @param plainText 明文
     * @param password 加密密钥
     * @return ENC(加密后的字符串)
     */
    public static String encryptForConfig(String plainText, String password) {
        String encrypted = encrypt(plainText, password);
        return "ENC(" + encrypted + ")";
    }

    /**
     * 命令行工具主方法
     * 用法：java EncryptionUtil <action> <text> <password>
     * action: encrypt 或 decrypt
     */
    public static void main(String[] args) {
        if (args.length != 3) {
            System.out.println("用法: java EncryptionUtil <encrypt|decrypt> <text> <password>");
            System.out.println("示例:");
            System.out.println("  加密: java EncryptionUtil encrypt mypassword mykey");
            System.out.println("  解密: java EncryptionUtil decrypt ENC(xxx) mykey");
            return;
        }

        String action = args[0];
        String text = args[1];
        String password = args[2];

        try {
            switch (action.toLowerCase()) {
                case "encrypt":
                    String encrypted = encryptForConfig(text, password);
                    System.out.println("原文: " + text);
                    System.out.println("密文: " + encrypted);
                    System.out.println("配置格式: " + encrypted);
                    break;
                    
                case "decrypt":
                    // 如果输入是ENC(xxx)格式，提取中间部分
                    String encryptedText = text;
                    if (text.startsWith("ENC(") && text.endsWith(")")) {
                        encryptedText = text.substring(4, text.length() - 1);
                    }
                    String decrypted = decrypt(encryptedText, password);
                    System.out.println("密文: " + text);
                    System.out.println("原文: " + decrypted);
                    break;
                    
                default:
                    System.out.println("错误: 操作类型必须是 encrypt 或 decrypt");
            }
        } catch (Exception e) {
            System.out.println("错误: " + e.getMessage());
        }
    }
} 